﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XBLIGUK2011Talk2.HUD
{
    public delegate void OnClickEvent(object sender);
    public delegate void OnChangeEvent(object sender);

    public class BasicHUD : GameComponent
    {
        public enum States
        {
            Hidden,
            Showing,
            Shown,
            Hiding
        }
        public States CurrentState;

        public SpriteBatch spriteBatch
        {
            get { return (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch)); }
        }

        protected List<HUDObject> items = new List<HUDObject>();

        public int Height, Width;
        public float Top,Left;

        public const float speed = 8.5f;

        public BasicHUD(Game game, int heigh, int width) : base(game)
        {
            Height = heigh;
            Width = width;
        }

        public void AddItem(HUDObject item)
        {
            item.Parent = this;
            items.Add (item);
        }

        public override void Update(GameTime gameTime)
        {
            switch (CurrentState)
            {
                case States.Hidden:
                    Top = -Height;
                    break;
                case States.Showing:
                    Top += speed;
                    if (Top >= 0)
                        CurrentState = States.Shown;
                    break;
                case States.Shown:
                    Top = 0;                    
                    break;
                case States.Hiding:
                    Top -= speed;
                    if(Top <= -Height)
                        CurrentState = States.Hidden;
                    break;
            }
            if (CurrentState != States.Hidden)
            {
                int cnt = items.Count;
                for (int i = 0; i < cnt; i++)
                    items[i].Update(gameTime);
            }
        }

        public virtual void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
            spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/HUD/HUDBackground"), new Rectangle((int)Left, (int)Top, Width, Height), Color.White);

            int cnt = items.Count;
            for (int i = 0; i < cnt; i++)
                items[i].Draw(gameTime);

            spriteBatch.End();
        }
    }
}
